package com.hxzy.servlet.pmsbrand;

import com.hxzy.entity.PmsBrand;
import com.hxzy.service.PmsBrandService;
import com.hxzy.service.impl.PmsBrandServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/*
 *  根据主键查询信息 显示到网页上
 */
@WebServlet(urlPatterns = "/pmsbrand/update")
public class update extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
        //接收网页地址传入的参数
        String value = req.getParameter("id");

        //查询数据  业务逻辑层
        PmsBrandService service=new PmsBrandServiceImpl();
        //查询数据
        PmsBrand pmsBrand = service.selectByPrimaryKey(Long.parseLong(value));
        /*
        * 把数据打包 转发给页面并且给予地址
        * */
        req.setAttribute("id",pmsBrand);
        req.getRequestDispatcher("/WEB-INF/pmsbrand/update.jsp").forward(req,res);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        res.setCharacterEncoding("UTF-8");
        res.setContentType("text/html;charset=UTF-8");

        PmsBrand pmsBrand=new PmsBrand();
        String name=req.getParameter("name");
        pmsBrand.setName(name);
        //首字母
        String firstLetter=req.getParameter("firstLetter");
        pmsBrand.setFirstLetter(firstLetter);
        //排序
        Integer sort=Integer.parseInt(req.getParameter("sort"));
        pmsBrand.setSort(sort);
        //是否为品牌制造商：0->不是；1->是
        Integer factoryStatus=Integer.parseInt(req.getParameter("factoryStatus"));
        pmsBrand.setFactoryStatus(factoryStatus);

        Integer showStatus=Integer.parseInt(req.getParameter("showStatus"));
        pmsBrand.setShowStatus(showStatus);
        //产品数量
        Integer productCount=Integer.parseInt(req.getParameter("productCount"));
        pmsBrand.setProductCount(productCount);
        //产品评论数量
        Integer productCommentCount=Integer.parseInt(req.getParameter("productCommentCount"));
        pmsBrand.setProductCommentCount(productCommentCount);
        //品牌logo
        String logo=req.getParameter("logo");
        pmsBrand.setLogo(logo);
        //专区大图
        String bigPic=req.getParameter("bigPic");
        pmsBrand.setBigPic(bigPic);
        //品牌故事
        String brandStory=req.getParameter("brandStory");
        pmsBrand.setBrandStory(brandStory);

        Long id=Long.parseLong(req.getParameter("id"));
        pmsBrand.setId(id);

        PmsBrandService service=new PmsBrandServiceImpl();
        boolean insert = service.updateByPrimaryKey(pmsBrand);

//        PrintWriter out=res.getWriter();
//        out.println( insert? "修改成功": "修改失败");
//        //输出刷新
//        out.flush();
//        out.close();

        String path=req.getContextPath();
        req.setAttribute("url",path+"/pmsbrand/search");
        //转发
        req.getRequestDispatcher("/WEB-INF/jump.jsp").forward(req,res);
    }
}
